import { config } from 'utils'
import dvarequest from '../utils/dvarequest'
import dvarequestnojson from '../utils/dvarequestnojson'

const { api } = config
const { deploy } = api
const { processType } = api
const { baseApi } = api
const { processDefinition } = api

export function fetch (values) {
  return dvarequest(`${baseApi}/process-deploy-detail`, {
    method: 'POST',
    body: JSON.stringify(values),
  })
}

export function remove (id) {
  return dvarequest(`${baseApi}/process-deploy-detail/${id.id}`, {
    method: 'DELETE',
  })
}

export function patch (id, values) {
  return dvarequest(`${processType}/${id}`, {
    method: 'PUT',
    body: JSON.stringify(values),
  })
}

export function create (values) {
  return dvarequest(`${processType}/add`, {
    method: 'POST',
    body: JSON.stringify(values),
  })
}

export function showDiagramModal (values) {
  return dvarequestnojson(`${processDefinition}/${values.currentItem.procDefId}/diagram`, {
    method: 'GET',
  })
}

export function configTaskNode (values) {
  return dvarequest(`${baseApi}/process-deploy-detail/config`, {
    method: 'PUT',
    body: JSON.stringify(values),
  })
}

export function deployProcess (values) {
  values.processTypeId = values.data.processTypeId
  values.systemCode = values.data.systemCode
  let formData = new FormData()
  for (let value in values) {
    formData.append(value, values[value])
  }
  formData.append('file', values.file.file)
  return dvarequest(`${deploy}/deployments?systemCode=${values.systemCode}&processTypeId=${values.processTypeId}`, {
    method: 'POST',
    body: formData,
    cache: 'no-cache',
  })
}
